<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1>购物车</h1>
  <div id="app">
     <table class="table" border="1">
    
      <thead>
        <tr>
          <th class="text-center">
            <input type="checkbox" v-model="isCheckAll" @change="allChange">全选
          </th>
          <th class="text-center">名称</th>
        </tr>
      </thead>
    
      <tbody>
        <tr v-for="(item,index) in cart">
          <td><input type="checkbox" v-model="item.checked" @change="itemChange"></td>
          <td>{{item.name}}</td>
        </tr>
      </tbody>
    
     </table>
  </div> 
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        // 控制全选
        isCheckAll: false,
        cart: [
          {name: 'huawei', price: 4999, num: 5},
          {name: 'xiaomi', price: 4599, num: 7},
        ]
      },
      created() {
        // 给源数据增加checked选择，用来控制选中
        this.cart.forEach(element => {
          element.checked = false
        });
      },
      methods: {
        // 修改全选按钮
        allChange() {
          // console.log(111,this.isCheckAll);
          // 每一项都要同步
          this.cart.forEach(item => {
            item.checked = this.isCheckAll
          })
        },
        // 每一项的变化
        itemChange() {
          // 每点击一次，就判断整个数组是否全都被选中
          this.isCheckAll = this.cart.every(item => {
            return item.checked
          })
        }
      },
    })
  </script>
</body>
</html>